<?php namespace Phpcmf\Controllers\Admin;

class Home extends \Phpcmf\Table {

    public function __construct(...$params)
    {
        parent::__construct(...$params);


        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('Auth')->_admin_menu(
                [
                    '订单管理' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-shopping-cart'],
                    '详情' => ['hide:'.APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/show', 'fa fa-edit'],
                ]
            ),
        ]);
    }

    public function add() {

        $id = (int)\Phpcmf\Service::L('input')->get('id');
        $order = \Phpcmf\Service::M('order', 'order')->info($id);
        if (!$order) {
            $this->_json(0, dr_lang('订单不存在'));
        } elseif ($order['sell_uid'] != $this->uid) {
            $this->_json(0, dr_lang('无权限操作'));
        }

        $at = \Phpcmf\Service::L('input')->get('at');
        \Phpcmf\Service::V()->assign([
            'order' => $order,
        ]);

        switch ($at) {


            case 'delivery':
                // 订单发货
                if (!$order['pay_id']) {
                    $this->_json(0, dr_lang('此订单还未付款'));
                }
                if (IS_AJAX_POST) {
                    if ($order['status'] == 9) {
                        $this->_json(0, dr_lang('此订单已经交易完成了'));
                    }
                    // 未发货，时后台为他发货
                    $data = \Phpcmf\Service::L('Input')->post('data');
                    !$data['deliver_name'] && $this->_json(0, dr_lang('快递名称必须填写'), ['field' => 'deliver_name']);
                    !$data['deliver_sn'] && $this->_json(0, dr_lang('快递单号必须填写'), ['field' => 'deliver_sn']);
                    !$data['note'] && $this->_json(0, dr_lang('备注信息必须填写'), ['field' => 'note']);
                    !$data['name'] && $this->_json(0, dr_lang('姓名必须填写'), ['field' => 'name']);
                    !$data['phone'] && $this->_json(0, dr_lang('电话号码必须填写'), ['field' => 'phone']);
                    !$data['address'] && $this->_json(0, dr_lang('详细地址必须填写'), ['field' => 'address']);
                    // 创建发货记录
                    $rt = \Phpcmf\Service::M('order', 'order')->fahuo($order, $data);
                    !$rt['code'] && $this->_json(0, $rt['msg']);
                    $this->_json(1, dr_lang('操作成功'));
                }
                break;

            case 'log':
                // 订单备注
                if (IS_AJAX_POST) {
                    $note = \Phpcmf\Service::L('input')->post('note');
                    !$note && $this->_json(0, dr_lang('备注内容必须填写'), ['field' => 'note']);
                    // 订单备注
                    \Phpcmf\Service::M('order', 'order')->add_log($order, $note);
                    $this->_json(1, dr_lang('操作成功'));
                }
                break;

            case 'close':
                // 订单关闭
                if ($order['status'] == 0) {
                    $this->_json(0, dr_lang('此订单已经关闭'));
                } elseif ($order['status'] == 9) {
                    $this->_json(0, dr_lang('此订单已经交易完成'));
                }

                if (IS_AJAX_POST) {
                    $note = \Phpcmf\Service::L('input')->post('note');
                    !$note && $this->_json(0, dr_lang('关闭原因必须填写'), ['field' => 'note']);
                    $order['status'] == 3 && $this->_json(0, dr_lang('此订单已经发货，无法关闭'));
                    // 订单备注
                    \Phpcmf\Service::M('order', 'order')->close($order, $note);
                    $this->_json(1, dr_lang('操作成功'));
                }

                break;

        }
        \Phpcmf\Service::V()->display('order_'.$at.'.html');exit;
    }


    public function show() {

        // 表单显示名称
        $this->name = dr_lang('订单');
        $this->is_list_data = 0;

        // 初始化数据表
        $this->_init(['table' => SITE_ID.'_order']);

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        list($tpl, $data) = $this->_Show($id);
        !$data && $this->_admin_msg(0, dr_lang('订单不存在'));

        $field = \Phpcmf\Service::M('order', 'order')->get_field(\Phpcmf\Service::M('app')->get_config(APP_DIR), $data['mid']);
        if ($field) {
            \Phpcmf\Service::L('Field')->app(APP_DIR);
            $myfield = \Phpcmf\Service::L('Field')->toform($id, $field, $data, 1);
        } else {
            $myfield = '';
        }

        \Phpcmf\Service::V()->assign([
            'order' => $data,
            'goods' => \Phpcmf\Service::M('Order', 'order')->get_item($id),
            'op_log' => \Phpcmf\Service::M('Order', 'order')->log($id),
            'op_url' => \Phpcmf\Service::L('Router')->url(APP_DIR.'/home/add', ['id' => $id]),
            'myfield' => $myfield,
        ]);
        \Phpcmf\Service::V()->display('order_show.html');
    }

    public function index() {

        // 模板前缀(避免混淆)
        $this->my_field = array(
            'sn' => array(
                'ismain' => 1,
                'name' => dr_lang('订单编号'),
                'fieldname' => 'sn',
                'fieldtype' => 'Text',
            ),
            'id' => array(
                'ismain' => 1,
                'name' => dr_lang('订单ID'),
                'fieldname' => 'id',
                'fieldtype' => 'Text',
            ),
            'name' => array(
                'ismain' => 1,
                'name' => dr_lang('收货人'),
                'fieldname' => 'name',
                'fieldtype' => 'Text',
            ),
            'phone' => array(
                'ismain' => 1,
                'name' => dr_lang('电话'),
                'fieldname' => 'phone',
                'fieldtype' => 'Text',
            ),

            'address' => array(
                'ismain' => 1,
                'name' => dr_lang('地址'),
                'fieldname' => 'address',
                'fieldtype' => 'Text',
            ),
            'remark' => array(
                'ismain' => 1,
                'name' => dr_lang('备注'),
                'fieldname' => 'remark',
                'fieldtype' => 'Text',
            ),
            'username' => array(
                'ismain' => 1,
                'name' => dr_lang('账户'),
                'fieldname' => 'username',
                'fieldtype' => 'Text',
            ),
            'uid' => array(
                'ismain' => 1,
                'name' => dr_lang('uid'),
                'fieldname' => 'uid',
                'fieldtype' => 'Text',
            ),
        );

        // 表单显示名称
        $this->name = dr_lang('订单');
        // 分类
        $type = [
            0 => [
                'name' => dr_lang('所有订单'),
                'icon' => '<i class="fa fa-shopping-cart"></i>',
                'url' => dr_url('order/home/index', ['tid' => 0]),
            ],
            1 => [
                'name' => dr_lang('未付款'),
                'icon' => '<i class="fa fa-rmb"></i>',
                'url' => dr_url('order/home/index', ['tid' => 1]),
            ],
            2 => [
                'name' => dr_lang('已付款'),
                'icon' => '<i class="fa fa-check-square-o"></i>',
                'url' => dr_url('order/home/index', ['tid' => 2]),
            ],
            3 => [
                'name' => dr_lang('已发货'),
                'icon' => '<i class="fa fa-truck"></i>',
                'url' => dr_url('order/home/index', ['tid' => 3]),
            ],
            4 => [
                'name' => dr_lang('已完成'),
                'icon' => '<i class="fa fa-check-circle"></i>',
                'url' => dr_url('order/home/index', ['tid' => 4]),
            ],
        ];

        $tid = (int)$_GET['tid'];
        switch ($tid) {

            case 1:
                $where = '`status` = 1';
                break;

            case 2:
                $where = '`status` = 2';
                break;

            case 3:
                $where = '`status` = 3';
                break;

            case 4:
                $where = '`status` = 9';
                break;

            default:
                $where = '';
                break;
        }


        // 初始化数据表
        $this->_init([
            'table' => SITE_ID.'_order',
            'field' => $this->my_field,
            'sys_field' => [],
            'order_by' => 'inputtime desc',
            'date_field' => 'inputtime',
            'list_field' => [],
            'where_list' => $where,
        ]);
        list($a, $data) = $this->_List(['tid' => $tid]);

        $list = [];
        foreach ($data['list'] as $t) {
            $t['goods'] = \Phpcmf\Service::M('order', 'order')->get_item($t['id']);
            $list[] = $t;
        }

        \Phpcmf\Service::V()->assign([
            'list' => $list,
            'type' => $type,
            'field' => $this->my_field,
        ]);
        \Phpcmf\Service::V()->display('order_list.html');
    }


    // 后台删除
    public function del() {

        // 删除时同步删除很多内容
        // 初始化数据表
        $this->_init(['table' => SITE_ID.'_order']);

        $this->_Del(
            \Phpcmf\Service::L('input')->get_post_ids(),
            null,
            function ($rows) {
                $ids = [];
                foreach ($rows as $t) {
                    $ids[] = $id = intval($t['id']);
                    \Phpcmf\Service::M()->table_site('order_log')->where('oid', $id)->delete();
                    \Phpcmf\Service::M()->table_site('order_item')->where('oid', $id)->delete();

                }
                return dr_return_data(1, 'ok');
            },
            \Phpcmf\Service::M()->dbprefix('member')
        );
    }

}
